package com.yixun.qfbao.controller.agency.weapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.yixun.qfbao.api.service.PremisesApiService;
import com.yixun.qfbao.apidto.GetAgentPremisesDynamicPageReqDto;
import com.yixun.qfbao.apidto.GetAgentPremisesDynamicPageResDto;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.request.PageRequest;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.premisesdynamic.PremisesDynamicPageResDto;
import com.yixun.qfbao.response.premisesdynamic.PremisesDynamicResDto;
import com.yixun.qfbao.xf.model.PremisesDynamic;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zilong.liu
 * @date 2020-9-10
 */
@Controller
@ResponseBody
@RequestMapping("/agency/weapp/premisesDynamic")
@Api(tags = {"分销小程序-楼盘动态"})
public class PremisesDynamicController {

	@Autowired
	PremisesApiService premisesApiService;
	
	
	@ApiOperation(notes = "分页获取楼盘动态", value="分页获取楼盘动态")
    @PostMapping("/getAgentPremisesDynamicPage")
    public BaseResponse<PremisesDynamicPageResDto> getAgentPremisesDynamicPage(@SuppressWarnings("rawtypes") @RequestBody @Valid PageRequest request) {
        BaseResponse<PremisesDynamicPageResDto> response = new BaseResponse<>();
        GetAgentPremisesDynamicPageReqDto reqDto1 = new GetAgentPremisesDynamicPageReqDto();
        reqDto1.setCompanyUuid(request.getCompanyUuid());
        reqDto1.setUserId(request.getUserId());
        reqDto1.setPageNo(request.getPageDTO().getPageNum());
        reqDto1.setPageSize(request.getPageDTO().getPageSize());
        reqDto1.setLoginType(request.getLoginType());
        
        GetAgentPremisesDynamicPageResDto premisesDynamicList = premisesApiService.getAgentPremisesDynamicPage(reqDto1);
        if(premisesDynamicList == null || premisesDynamicList.getPage() == null) {
        	response.setCode(premisesDynamicList.getResult());
            response.setMsg(premisesDynamicList.getResultMsg());
            return response;
        }
        List<PremisesDynamic> premisesDynamics = premisesDynamicList.getPage().getRows();
        
        //分销小程序，需要根据查询咨询人员电话号码
        List<Long> premisesIds = new ArrayList<>();
        for (PremisesDynamic premisesDynamic : premisesDynamics) {
        	premisesIds.add(premisesDynamic.getPremisesId());
		}
        Map<Long, PartnerInfo> caseMemberMap = premisesApiService.getPhoneCaseMemberInfoByPremisesInfo(premisesIds);
        for (PremisesDynamic premisesDynamic : premisesDynamics) {
        	Long premisesId = premisesDynamic.getPremisesId();
        	premisesDynamic.setCaseMemberPhone(caseMemberMap.get(premisesId) == null? "": caseMemberMap.get(premisesId).getPhone());
		}
        PremisesDynamicPageResDto premisesDynamicPageResDto = new PremisesDynamicPageResDto();
        if(!CollectionUtils.isEmpty(premisesDynamics)) {
        	premisesDynamicPageResDto.addPremisesDynamicResDtos(premisesDynamics);
        }
        response.setData(premisesDynamicPageResDto);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }
	
	@ApiOperation(notes = "详情", value="动态详情")
    @PostMapping("/getAgentPremisesDynamicDetail")
    public BaseResponse<PremisesDynamicResDto> getAgentPremisesDynamicDetail(@RequestBody @Valid BaseRequest<GenericIdRequest> request) {
        BaseResponse<PremisesDynamicResDto> response = new BaseResponse<>();
        PremisesDynamic premisesDynamic = premisesApiService.getAgentPremisesDynamicDetail(request.getData().getId());
        PremisesDynamicResDto premisesDynamicResDto = new PremisesDynamicResDto(premisesDynamic, true);
        response.setData(premisesDynamicResDto);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }
	
	public static void main(String[] args) {
		Map<Long, Object> map = new HashMap<>();
		map.put(new Long(123123), "12313");
		System.out.println(map.get(new Long(123123)));
	}
}
